package cn.yx.common.i18n.configuration;

import cn.yx.common.i18n.constant.I18nConstant;
import cn.yx.common.i18n.properties.I18nMessageProperties;
import cn.yx.common.redis.service.RedisService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * <p>i18n语言配置类</p>
 *
 * @author Wgssmart
 */
@Configuration
@EnableConfigurationProperties(I18nMessageProperties.class)
@Slf4j
public class I18nMessageConfiguration {

    private final I18nMessageProperties i18nMessageProperties;

    private final RedisService redisService;

    public I18nMessageConfiguration(I18nMessageProperties i18nMessageProperties, RedisService redisService) {
        this.i18nMessageProperties = i18nMessageProperties;
        this.redisService = redisService;
    }

    /**
     * 获取配置的语言并缓存到Redis
     */
    @Bean
    public void i18nLocale() {
        String lang = i18nMessageProperties.getLang();
        log.info("i18n.message.lang={}", lang);
        redisService.set(I18nConstant.I18N_LANG_KEY, lang);
    }

}